package restdom.validation.ruleengine;

import java.util.List;

import org.springmodules.jsr94.Jsr94Exception;
import org.springmodules.jsr94.rulesource.RuleSource;
import org.springmodules.validation.bean.rule.AbstractValidationRule;
import org.springmodules.validation.util.condition.Condition;

import restdom.ruleengine.RuleSourceResolver;

public class RuleEngineValidationRule extends AbstractValidationRule {

	public static final String DEFAULT_ERROR_CODE = "ruleengine";

	private String bindUri;

	private List<Object> helpers;

	private RuleSource ruleSource;

	public RuleEngineValidationRule(String bindUri, String source, Class clazz,
			RuleSourceResolver resolver, List<Object> helpers) {
		super(DEFAULT_ERROR_CODE);
		this.bindUri = bindUri;
		this.helpers = helpers;
		try {
			this.ruleSource = resolver
					.resolveRuleSource(bindUri, source, clazz);
		} catch (Exception e) {
			throw new Jsr94Exception("Invalid rule source: bindUri=[" + bindUri
					+ "], source=[" + source + "]");
		}
	}

	public Condition getCondition() {
		return new RuleEngineCondition(ruleSource, bindUri, helpers);
	}

	public String getBindUri() {
		return bindUri;
	}

	public void setBindUri(String bindUri) {
		this.bindUri = bindUri;
	}
}
